package com.jkoss.action.jy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jkoss.biz.IEmpsBiz;
import com.jkoss.biz.jy.JyCompsBiz;
import com.jkoss.biz.jy.StucompanyBiz;
import com.jkoss.pojo.jy.JiuyeGS;
import com.jkoss.pojo.jy.Stucompany;
import com.jkoss.pojo.oa.Emps;
import com.jkoss.tool.DbCommonMethod;
import com.jkoss.tool.Page;

@Controller
@RequestMapping("/jy")
public class StucompanyAction {
    
	@Autowired
	private StucompanyBiz  scBiz;
	
	@Autowired
	private JyCompsBiz jybiz;
	
	@Autowired
    private IEmpsBiz  ebiz;
	
	//-------------------------就业企业跟进表
	
	//显示全部
	@RequestMapping("listSc")
	public String listSc(HttpServletRequest req,Page<Stucompany> page,Integer jyid,Integer eid){
		
		if(page==null){
			page=new Page<Stucompany>();
		}
		page.setResults(scBiz.listPageSc(page,jyid,eid));
		
		req.setAttribute("page", page);
		
		//查询全部公司
		List<JiuyeGS>  gss=jybiz.listAll();
		req.setAttribute("gss", gss);
		//查询员工
		List<Emps> emps=ebiz.findAllEmp();
		req.setAttribute("emps", emps);
		
		req.setAttribute("jyid", jyid);
		req.setAttribute("eid", eid);
		return "listSc.jsp";
		
	}
	
	//查看单个
	@RequestMapping("oneSc")
	public String oneSc(HttpServletRequest req,Integer jygjID){
		Stucompany sc=scBiz.findById(jygjID);
		req.setAttribute("sc", sc);
		return "shSc.jsp";	
	}
	
	//添加
	@RequestMapping("addSc")
	public String addSc(HttpServletRequest req,Stucompany sc){
		
		Stucompany s =scBiz.findByJyid(sc.getJyid());
		if(s!=null){
			req.setAttribute("msg", "已经存在跟进公司");
			return listSc(req,null,null,null);
		}else{
		   if(sc.getJygjDate().after(new Date())){
				sc.setJygjDate(new Date());
			}
			String msg=scBiz.addSc(sc);
			req.setAttribute("msg", msg);
		
		}	
		
		
		return listSc(req,null,null,null);
		
	}
	
	//删除
	@RequestMapping("delSc")
	public String delSc(HttpServletRequest req,Integer jygjID){
		
		String msg=scBiz.delSc(jygjID);
		req.setAttribute("msg", msg);
		return listSc(req,null,null,null);
	}
	
	//发送JSON数据
	@RequestMapping(value="/toUpSc.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String toUpSc(Integer jygjID){
		
		System.out.println(scBiz.findById(jygjID).getJygjDate());
		return JSONObject.toJSONString(scBiz.findById(jygjID)).toString();
		
	}
	
	//更改
	@RequestMapping("upSc")
	public String upSc(HttpServletRequest req,Stucompany sc){
		
		if(sc.getJygjDate().after(new Date())){
			sc.setJygjDate(new Date());
		}
		String msg=scBiz.upSc(sc);
		req.setAttribute("msg", msg);
		
		return listSc(req,null,null,null);
		
	}
}
